/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package avl.sv.shared.Annotations;

import avl.sv.shared.SlideReference;
import java.io.Serializable;
import java.util.ArrayList;
import org.jdesktop.swingx.treetable.DefaultMutableTreeTableNode;
import org.jdesktop.swingx.treetable.MutableTreeTableNode;

/**
 *
 * @author benbryan
 */
public class AnnotationSet extends DefaultMutableTreeTableNode implements Serializable  {
    SlideReference slideReference;
    
    public SlideReference getSlideReference() {
        return slideReference;
    }
       
    public byte[] getHash(){
        return slideReference.hash;
    }
    
    public AnnotationSet(SlideReference slideReference) {
        super(slideReference.slideName);
        this.slideReference = slideReference;
    }
        
    public ArrayList<Annotation> getAnnotations() {       
        if (children == null){
            return new ArrayList<>();
        }
        ArrayList<Annotation> annotations = new ArrayList<>();
        for (Object r:children){
            if (r instanceof Annotation){
                annotations.add((Annotation)r);
            }
        }
        
        return annotations;
    }

    @Override
    public void add(MutableTreeTableNode child) {
        // make sure child is of annotation and that its name is unique
        if (child instanceof Annotation){
            Annotation newAnno = (Annotation) child;
            if (containsAnnotation(newAnno.name)){
                super.add(newAnno);
                return;
            }
            for (int i = 0; i < 1000; i++){
                String name = newAnno.name.concat(String.valueOf(i));
                if (containsAnnotation(name)){
                    newAnno.name = name;
                    super.add(newAnno);
                    return;
                }
            }
        }
    }
    
    public boolean containsAnnotation(String s) {
        for (Annotation anno:getAnnotations()) {
            if (s.equals(anno.name)) {
                return false;
            }
        }
        return true;
    }
    
    
}
